package Finance::Pershing::StandardFile::SELI;

use Moose;
use Finance::Pershing::StandardFiles::Utils qw/trim parse_date/;

extends 'Finance::Pershing::StandardFile';

our $VERSION = '0.001000';

our $detail_a = qr/^SLA(\d{8})(.{9})(.{3})(.{9})(.)(.{9})(.)(.).{205}X$/;

sub is_refresh {
  1;
}
sub is_delta {
  0;
}

sub _build_records {
  my $self = shift;
  my %records;
  defined(my $line = $self->next_line) or
    $self->error("File ended prematurely on HEADER");

  while ( $line =~ /$detail_a/ ){
    my $household_id = trim $4;
    my $rec = $records{$household_id} ||= {household_id => $household_id};

    $rec->{mail_to_account} ||= trim $2;
    $rec->{ibd_number} ||= trim $3;
    $rec->{group_purpose} ||= trim $5;
    $rec->{accounts} ||= [];
    push(@{$rec->{accounts}}, trim $6);

    $rec->{principal_interested_party_ind} ||= trim $8 if($8 ne ' ');

    if ($7 ne ' ') {
      $rec->{gets_summary_page} = (trim $7 eq 'Y') ? 1 : 0; 
    }

    defined($line = $self->next_line) or
      $self->error("File ended prematurely on DETAIL A");
  }
  if ( $self->_process_footer($line) ) {
    return [ @records{sort(keys %records)}];
  } else {
    $self->error("Got '$line' where DETAIL/FOOTER record was expected");
  }
  $self->error("Recieved no FOOTER record. File possibly truncated");
}

__PACKAGE__->meta->make_immutable;

1;

__END__;

=head1 NAME

Finance::Pershing::StandardFile::SELI - Account Profile (SELI/SelectLink) File

=head1 SYNOPSIS

=head1 METHODS

=head2 _build_records

=over 4

=item mail_to_account

=item ibd_number

=item group_id

=item group_purpose

=item acctnum

=item gets_summary_page

=item principal_interested_party_ind

=back

=head1 AUTHOR & LICENSE

Please see L<Finance::Pershing::StandardFile> for more information.

=cut
